//
//  ClassTypeObject.m
//  huashiedu
//
//  Created by oceans on 2018/9/14.
//  Copyright © 2018年 hema. All rights reserved.
//

#import "ClassTypeObject.h"
#import "HttpRequest.h"
#import "OAuthAPI.h"

@implementation ClassTypeArrayModel

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        [self setup:dic];
    }
    return self;
}

- (void)setup:(NSDictionary *)dic {
    _name = [dic objectForKey:@"name"];
    __id = [[dic objectForKey:@"id"] intValue];
    if ([dic objectForKey:@"children"] != nil && (NSNull *)[dic objectForKey:@"children"] != [NSNull null]) {
        NSArray *array = [dic objectForKey:@"children"];
        NSMutableArray *models = [NSMutableArray arrayWithCapacity:array.count];
        for (NSDictionary *dic in array) {
            ClassTypeArrayModel *model = [[ClassTypeArrayModel alloc] initWithDic:dic];
            [models addObject:model];
        }
        self.children = models;
    }
}

@end

static ClassTypeObject *shared;

@implementation ClassTypeObject

+ (instancetype)shared {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [[super alloc] init];
    });
    return shared;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t token;
    dispatch_once(&token, ^{
        shared = [super allocWithZone:zone];
    });
    return shared;
}

- (id)copyWithZone:(NSZone *)zone {
    return shared;
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _allTypes = [NSMutableArray array];
        [self loadClassTypes];
    }
    return self;
}

- (void)loadClassTypes {
    
    NSUserDefaults *stand = [NSUserDefaults standardUserDefaults];
    NSArray *result = [stand objectForKey:@"class_categorys_cache"];
    if (result && result.count > 0) {
        
        for (NSDictionary *dic in result) {
            
            ClassTypeArrayModel *model = [[ClassTypeArrayModel alloc] initWithDic:dic];
            [_allTypes addObject:model];
        }
    }
}

- (void)requestClassTypes:(void (^)(NSArray<ClassTypeArrayModel *> *))finishBlock {
    if ([OAuthAPI getAccessToken].length == 0) {
        return;
    }
    __weak typeof(self) weakself = self;
    [HttpRequest getURL:kget_class_category paras:nil showProgress:NO successful:^(id  _Nullable result) {
        NSLog(@"%@", result);
        [weakself.allTypes removeAllObjects];
        for (NSDictionary *dic in result[@"data"]) {
            ClassTypeArrayModel *model = [[ClassTypeArrayModel alloc] initWithDic:dic];
            [weakself.allTypes addObject:model];
        }
        [weakself save:result[@"data"]];
        if (finishBlock) {
            finishBlock(weakself.allTypes);
        }
    } failed:nil];
}

- (void)save:(NSArray *)array {
    
    [[NSUserDefaults standardUserDefaults] setObject:array forKey:@"class_categorys_cache"];
}

- (int)findIdWithName:(NSString *)name {
    
    if (_allTypes.count == 0) {
        return 0;
    }
    for (ClassTypeArrayModel *model in _allTypes) {
        if ([model.name isEqualToString:name]) {
            return model._id;
        }
    }
    return 0;
}

@end
